function main() {
    // 线索移交
    const self = this;
    debugger

    const clueTaskId = self.$$("widget_list_picker_78uco4").cValue;
    const objectIdValue = (self.state.extraParams || {})["objectId"];
    const formEle = self.$$("form_part_fvxfbx").$$getRenderElement();

    // 督办下发（law_std_sup_task 已发送）
    self.$$("number_input_hib6mp").cValue = 1;

    formEle.onSave().then(res => {
        console.log(res);
        const taskId = res.result['ds_tbl:t_91f06a36e9df4bf0981df72a4a2417f2-9e5a'].id;
        // const objectId = res.result['ds_tbl:t_91f06a36e9df4bf0981df72a4a2417f2-9e5a'].tree_single_select_93hk4h.replace('unit', '');
        const objectId = self.$$("number_input_cwv6ld").cValue;
        const objectName = self.$$("tree_single_select_93hk4h").selectedData[0].name;

        // 新增下发对象
        let taskData = {
            taskId: taskId,
            objectId: objectId,
            objectName: objectName
        }

        let username = api.USER_INFO().name;

        // 新增下发办理流程（监督对象）
        let handleRecord = {
            replyId: taskId,
            recordType: 2,
            recordTitle: "督办登记",
            recordContent: `督办人员${username}登记了督办事项。`,
            relationReplyId: ""
        }

        // 编辑操作
        if (objectIdValue) {
            updateTaskObject(objectIdValue, objectId, objectName).then(res => {
                if (clueTaskId) {
                    // 新增线索办理流程（线索办理经过，增加对象下发）
                    let handleRecordClue = {
                        replyId: clueTaskId,
                        recordType: 1,
                        recordTitle: "督办下发",
                        recordContent: `督办人员${username}下发督办事项至${objectName}办理。`,
                        relationReplyId: ""
                    }
                    insertHandleRecord(handleRecordClue).then(resClue => {
                        let handleRecordTask = {
                            replyId: taskId,
                            recordType: 2,
                            recordTitle: "督办下发",
                            recordContent: `督办人员${username}下发督办事项至${objectName}办理。`,
                            relationReplyId: ""
                        }
                        insertHandleRecord(handleRecordTask).then(resTask => {
                            updateClueStatus(self, clueTaskId);
                        });
                    })
                } else {
                    let handleRecordTask = {
                        replyId: taskId,
                        recordType: 2,
                        recordTitle: "督办下发",
                        recordContent: `督办人员${username}下发督办事项至${objectName}办理。`,
                        relationReplyId: ""
                    }
                    insertHandleRecord(handleRecordTask).then(resTask => {
                        let pageListEle = window.taskPageList;
                        // 获取列表渲染页实例
                        const listRenderView = pageListEle.$$getRenderElement();
                        // 获取当前显示的组件，表格或者卡片
                        const currentListEle = listRenderView.$$getListElement();
                        // 调用刷新方法
                        currentListEle.$$refreshData();
                        notifyParentAndClose(self)
                    });
                }
            });
        } else {
            insertTaskObject(taskData).then(res => {
                insertHandleRecord(handleRecord).then(r => {
                    if (clueTaskId) {
                        // 新增线索办理流程（线索办理经过，增加对象下发）
                        let handleRecordClue = {
                            replyId: clueTaskId,
                            recordType: 1,
                            recordTitle: "督办下发",
                            recordContent: `督办人员${username}下发督办事项至${objectName}办理。`,
                            relationReplyId: ""
                        }
                        insertHandleRecord(handleRecordClue).then(resClue => {
                            let handleRecordTask = {
                                replyId: taskId,
                                recordType: 2,
                                recordTitle: "督办下发",
                                recordContent: `督办人员${username}下发督办事项至${objectName}办理。`,
                                relationReplyId: ""
                            }
                            insertHandleRecord(handleRecordTask).then(resTask => {
                                updateClueStatus(self, clueTaskId);
                            });
                        })
                    } else {
                        let handleRecordTask = {
                            replyId: taskId,
                            recordType: 2,
                            recordTitle: "督办下发",
                            recordContent: `督办人员${username}下发督办事项至${objectName}办理。`,
                            relationReplyId: ""
                        }
                        insertHandleRecord(handleRecordTask).then(resTask => {
                            let pageListEle = window.taskPageList;
                            // 获取列表渲染页实例
                            const listRenderView = pageListEle.$$getRenderElement();
                            // 获取当前显示的组件，表格或者卡片
                            const currentListEle = listRenderView.$$getListElement();
                            // 调用刷新方法
                            currentListEle.$$refreshData();
                            notifyParentAndClose(self)
                        });
                    }

                });
            })
        }


    });

}

function updateTaskObject(objectIdValue, objectId, objectName) {
    return new Promise((resolve, reject) => {
        api.EXEC_FORMULA("API_CALL('ds_api:deef77e2-7e67-4bae-acbd-cbd468cfca82-9e5a', 'userId',USER_ID(), 'result.uid')").then(res => {
            let userId = res.result;
            api.DATA_SOURCE_EXECUTE("ds_mod:bcc76ab7-aa47-483a-bb7a-bd074476e003", "ddcat", [], [], "").then(res => {
                let unit_id = res.result.uid
                api.EXEC_FORMULA("UPDATE('ds_tbl:t_c232a327f2a44a8dbfa0c5a1c74a2026-9e5a', QUERY_EQ('id','" + objectIdValue + "'), '', ['opt_human_id', '" + userId + "','lead_unit_id','" + unit_id + "','object_id','" + objectId + "','object_name','" + objectName + "'])").then((res) => {
                    console.log(res);
                });
            });
        });
    });
}

/**
 * law_std_sup_task_object 执法标准督办任务流转对象表
 * 督办时候 初次下发
 * 在个案线索中只能是下发，不能是编辑状态
 * receive_status 1 '已发送'
 * @param clueData
 * @returns {Promise<unknown>}
 */
function insertTaskObject(clueData) {
    return new Promise((resolve, reject) => {
        api.EXEC_FORMULA("API_CALL('ds_api:deef77e2-7e67-4bae-acbd-cbd468cfca82-9e5a', 'userId',USER_ID(), 'result.uid')").then(res => {

            let userId = res.result;
            api.DATA_SOURCE_EXECUTE("ds_mod:bcc76ab7-aa47-483a-bb7a-bd074476e003", "ddcat", [], [], "").then(res => {
                let unit_id = res.result.uid
                const insertTaskObjects = [
                    ['task_id', clueData.taskId],
                    ['opt_human_id', `'${userId}'`],
                    ['lead_unit_id', `'${unit_id}'`],
                    ['object_id', `'${clueData.objectId}'`],
                    ['object_name', `'${clueData.objectName}'`],
                    ['object_type', '1'],
                    ['receive_status', '0'],
                    ['finish_status', '0'],
                    ['delete_flag', `'0'`],
                    ['create_time', 'NOW()'],
                    ['create_human_id', `'${userId}'`],
                ];

                const formattedFields = insertTaskObjects.map(([key, value]) => {
                    return `'${key}',${value}`;
                });

                const law_std_sup_task_object = "ds_tbl:t_c232a327f2a44a8dbfa0c5a1c74a2026-9e5a";
                const insertSql = `INSERT('${law_std_sup_task_object}', [${formattedFields.join(',')}])`;
                console.log(insertSql);


                api.EXEC_FORMULA(insertSql).then(res => {
                    console.log(res);
                    resolve(res);
                })
            })
        })
    });
}


/**
 * law_std_sup_clue
 * 修改线索状态 督办
 * */
function updateClueStatus(self, taskId) {

    const law_std_sup_clue = "ds_tbl:t_560875aad5054fa99e5e9cdd1c703828-9e5a";
    api.EXEC_FORMULA("UPDATE('" + law_std_sup_clue + "', QUERY_EQ('id', '" + taskId + "'), '', ['status', '" + 3 + "'])").then(() => {

        console.log("res", "执行结果,修改成功！");
        let pageListEle = window.cluePageList;
        // 获取列表渲染页实例
        const listRenderView = pageListEle.$$getRenderElement();
        // 获取当前显示的组件，表格或者卡片
        const currentListEle = listRenderView.$$getListElement();
        // 调用刷新方法
        currentListEle.$$refreshData();
        notifyParentAndClose(self)

    }).catch(err => {
        self.$message.error(err.message); // 显示错误提示信息
    });
}

/**
 * 处理经过
 * @param recordData
 * @returns {Promise<unknown>}
 */
function insertHandleRecord(recordData) {
    return new Promise((resolve, reject) => {
        api.EXEC_FORMULA("API_CALL('ds_api:deef77e2-7e67-4bae-acbd-cbd468cfca82-9e5a', 'userId',USER_ID(), 'result.uid')").then(res => {
            let userId = res.result;
            const insertHandleRecords = [
                ['record_uid', 'UUID()'],
                ['relation_id', `'${recordData.replyId}'`],
                ['record_type', `'${recordData.recordType}'`],
                ['record_title', `'${recordData.recordTitle}'`],
                ['record_content', `'${recordData.recordContent}'`],
                ['create_time', 'NOW()'],
                ['create_human_id', `'${userId}'`],
                ['relation_reply_id', `'${recordData.relationReplyId}'`]
            ];

            const formattedFields = insertHandleRecords.map(([key, value]) => {
                return `'${key}',${value}`;
            });

            const law_std_sup_handle_record = "ds_tbl:t_0ff47ea0e3a74ffdbc274409e7103e7e-9e5a";
            const insertSql = `INSERT('${law_std_sup_handle_record}', [${formattedFields.join(',')}])`;
            console.log(insertSql);


            api.EXEC_FORMULA(insertSql).then(res => {
                console.log(res);
                resolve(res);
            })
        })
    });


}

    function notifyParentAndClose(self) {
        const pageRenderElement = self.$$root().$parent.$parent.$parent;
        if (window.parent) {
            window.parent.postMessage(
                JSON.stringify({event: "close-dialog", param: {XXX: "test"}}),
                "*"
            );
        }
    }
